package com.wondersgroup.framework.sample.combination.action;

import com.wondersgroup.framework.core.bo.Page;
import com.wondersgroup.framework.core.web.struts2.action.BaseExcelAction;
import com.wondersgroup.framework.core.web.vo.VOUtils;
import com.wondersgroup.framework.core5.model.vo.ValueObject;
import com.wondersgroup.framework.sample.combination.vo.UserQueryVO;
import com.wondersgroup.framework.sample.component.data.UserData;
import com.wondersgroup.framework.sample.component.form.vo.UserVO;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.logging.Log;

public class PersonInfoAction extends BaseExcelAction
{
  private static final String prefix = "{\"success\":true, result:[";
  private static final String suffex = "]}";
  public UserVO userVO = new UserVO();

  public ValueObject getValueObject()
  {
    return this.userVO;
  }

  public UserVO getUserVO() {
    return this.userVO;
  }

  public void setUserVO(UserVO userVO) {
    this.userVO = userVO;
  }

  public String query()
  {
    UserQueryVO queryVO = new UserQueryVO();
    queryVO.setId(this.servletRequest.getParameter("id"));
    queryVO.setName(this.servletRequest.getParameter("name"));
    queryVO.setCensus(this.servletRequest.getParameter("census"));
    queryVO.setSex(this.servletRequest.getParameter("sex"));
    queryVO.setQuery(this.servletRequest.getParameter("query"));

    int start = 0;
    int size = 10;
    String pStart = this.servletRequest.getParameter("start");
    String pSize = this.servletRequest.getParameter("limit");
    if (pStart != null)
      start = Integer.parseInt(pStart);

    if (pSize != null) {
      size = Integer.parseInt(pSize);
    }

    List result = new UserData(this.servletRequest).queryUser(queryVO);
    String json = getJSON(start, size, result, UserVO.class);

    this.logger.info(json);
    super.createJSonData(json);
    return "ajax";
  }

  public String querynopage()
  {
    UserQueryVO queryVO = new UserQueryVO();
    queryVO.setId(this.servletRequest.getParameter("id"));
    queryVO.setName(this.servletRequest.getParameter("name"));
    queryVO.setCensus(this.servletRequest.getParameter("census"));
    queryVO.setSex(this.servletRequest.getParameter("sex"));
    queryVO.setQuery(this.servletRequest.getParameter("query"));

    List result = new UserData(this.servletRequest).queryUser(queryVO);
    String json = "{\"result\":" + VOUtils.getJsonDataFromCollection(result) + "}";

    this.logger.info(json);
    super.createJSonData(json);
    return "ajax";
  }

  public String load() {
    String id = getServletRequest().getParameter("id");
    UserVO result = new UserData(getServletRequest()).loadUserById(id);
    String json = "{\"success\":true, result:[" + VOUtils.getJsonData(result) + "]}";
    this.logger.info(json);
    super.createJSonData(json);
    return "ajax";
  }

  public String update() {
    boolean flag = true;
    String modifiedData = getServletRequest().getParameter("modifiedData");
    UserVO dataVO = (UserVO)VOUtils.getBeanFromJsonData(modifiedData, UserVO.class);
    UserData userData = new UserData(getServletRequest());
    if ((dataVO.getId() == null) || (dataVO.equals(""))) {
      int size = userData.queryUser(null).size();
      int id = size;
      dataVO.setId(Integer.toString(id));
    }
    userData.saveUser(dataVO);
    String json = "{success:" + flag + "}";
    super.createJSonData(json);
    return "ajax";
  }

  public String delete() {
    boolean flag = true;
    String deleteData = getServletRequest().getParameter("deleteData");
    UserVO dataVO = (UserVO)VOUtils.getBeanFromJsonData(deleteData, UserVO.class);
    new UserData(getServletRequest()).removeUser(dataVO.getId());
    String json = "{success:" + flag + "}";
    super.createJSonData(json);
    return "ajax";
  }

  private boolean isNull(String parameter)
  {
    return ((parameter == null) || (parameter.equals("")));
  }

  private String getJSON(int start, int size, List result, Class clazz)
  {
    Page page = new Page();
    List fresult = new ArrayList();

    if (result.size() < size)
      size = result.size();

    int loop = start + size;
    if (start + size > result.size())
      loop = result.size();

    for (int i = start; i < loop; ++i)
      if (i < result.size())
        fresult.add(result.get(i));


    if (size == 0) {
      size = 1;
    }

    page.setCurrentPageNo(start / size + 1);
    page.setCurrentPageSize(size);
    page.setResult(fresult);
    page.setTotalSize(result.size());
    String json = VOUtils.getJsonDataFromPage(page, clazz);
    return json;
  }

  public List getExportDataList()
  {
    UserQueryVO queryVO = new UserQueryVO();
    queryVO.setId(this.servletRequest.getParameter("id"));
    queryVO.setName(this.servletRequest.getParameter("name"));
    queryVO.setCensus(this.servletRequest.getParameter("census"));
    queryVO.setSex(this.servletRequest.getParameter("sex"));

    return new UserData(this.servletRequest).queryUser(queryVO);
  }

  public Object getExportSummaryData()
  {
    return null;
  }
}